﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using SqlsugarApiDemo.Models;

namespace SqlsugarApiDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdminTestController : ControllerBase
    {
        #region 构造函数依赖注入SqlSugar
        /// <summary>
        /// SqlSugar注入属性
        /// </summary>
        public ISqlSugarClient Db { get; }

        //常规写法：
        //public AdminTestController(ISqlSugarClient db) { Db = db; }

        //简化写法
        //符号=>的作用释义：
        //1.定义只读属性:public string Sex => "男"; 《 == 》 public string Sex { get;  } = "男"  
        //2.Lambda表达式，匿名委托 如下类似
        public AdminTestController(ISqlSugarClient db) => (Db) = db;

        #endregion

        //[ApiExplorerSettings(GroupName = "V1")]
        [HttpPost(Name = "AddPizza")]
        public bool AddPizza(Pizza pizza)
        {
            List<Pizza> list = Db.Queryable<Pizza>().Where(a => a.Id == pizza.Id).ToList();
            if (list.Count <= 0)
            {
                return Db.Insertable(pizza).ExecuteCommand() > 0;
            }
            return false;
        }

        [HttpGet("GetPizzaInfo")]
        /// <summary>
        /// 查询所有的PizzaInfo信息
        /// </summary>
        /// <returns></returns>
        public List<PizzaInfo> GetPizzaInfoAll()
        {
            List<PizzaInfo> list = Db.Queryable<PizzaInfo>().ToList();
            return list;
        }
    }
}
